/**
 * File Name   : ConfigLoader.java
 * Description : 
 */
package com.hh.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Properties配置文件加载
 * @author 冯昌硕
 * @create 2013-1-26 上午9:45:06
 */
public class ConfigLoader {

    private static final Logger log = Logger.getLogger(ConfigLoader.class);
    
    private Properties properties;
    
    public ConfigLoader() {
        properties = new Properties();
    }
    
    /**
     * 功能：初始化操作，加载conf.properties配置文件
     * @create 2012-10-22 下午9:54:10
     */
    public boolean load(String fileName) {
        log.debug("开始加载配置文件：" + fileName);
        InputStream cfgFileIs = null;
        try {
            
            String currPath = this.getClass().getResource("/").getPath();
            log.debug("currPath=["+ currPath +"]");
            
            // 取conf.properties配置文件目录
            //String userDir = System.getProperty("user.dir");
            //log.debug("user.dir=["+ userDir +"]");
            String path = currPath + File.separator + ".." + File.separator + "conf" + File.separator + fileName;
            log.debug("config file path=["+ path +"]");
            
            cfgFileIs = new BufferedInputStream(new FileInputStream(path));
            properties.load(cfgFileIs);
            
            return true;
        } catch (IOException e) {
            log.debug("加载配置文件" + fileName + " IOException异常：" + e.toString());
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            log.debug("加载配置文件" + fileName + " Exception异常：" + e.toString());
            e.printStackTrace();
            return false;
        } finally {
            if(cfgFileIs != null) {
                try {
                    cfgFileIs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
        }
    }
    
    /**
     * 功能：从Properties中取指定Key的值
     * @param key 键
     * @return 值
     * @create 2012-10-22 下午10:21:13
     */
    public String getValue(String key) {
        return properties.getProperty(key);
    }
    
}
